package im.starchat.chat.activity.profile;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import java.util.Arrays;

import butterknife.ButterKnife;
import butterknife.InjectView;
import im.starchat.chat.R;
import im.starchat.chat.XLApplication;
import im.starchat.chat.activity.BaseActivity;
import im.starchat.chat.adapter.PreviewAdapter;
import im.starchat.chat.fragment.PreviewFragment;
import im.starchat.chat.rest.RestClient;
import im.starchat.chat.rest.model.PreviewBean;
import im.starchat.chat.rest.model.api.PreviewResponse;
import im.starchat.chat.utils.PreferencesStorageUtil;
import im.starchat.chat.view.swipeRefreshLayout.SwipeRefreshLayout;
import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;


/**
 * 我的预约列表
 */
public class MyPreviewListActivity extends BaseActivity implements View.OnClickListener, SwipeRefreshLayout.OnRefreshListener, SwipeRefreshLayout.OnLoadListener{

    @InjectView(R.id.parade_list_back)
    ImageView mBackBtn;
    @InjectView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    @InjectView(R.id.swipe_refresh_layout_list)
    SwipeRefreshLayout mSwipeRefreshLayout;

    private PreviewAdapter mPreviewAdapter;
    private LinearLayoutManager mLayoutManager;
    private int mCurrentPage = RestClient.DEFAULT_PAGE;
    private boolean mHasMoreData = true;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_myparadelist);
        ButterKnife.inject(this);
        mBackBtn.setOnClickListener(this);

        mSwipeRefreshLayout.setOnRefreshListener(this);
        mSwipeRefreshLayout.setOnLoadListener(this);
        mSwipeRefreshLayout.setTopColor(R.color.refresh_color_01, R.color.refresh_color_02,
                R.color.refresh_color_02, R.color.refresh_color_01);
        mSwipeRefreshLayout.setBottomColor(R.color.color_transparent, R.color.color_transparent,
                R.color.color_transparent, R.color.color_transparent);
        mSwipeRefreshLayout.setMode(SwipeRefreshLayout.Mode.BOTH);
        mSwipeRefreshLayout.setLoadNoFull(true);

        mRecyclerView.setHasFixedSize(true);
        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mPreviewAdapter = new PreviewAdapter(this, new PreviewFragment.OnPreViewCallBack() {
            @Override
            public void onAppointments(String preview_id, int position) {
                appointments(preview_id, position);
            }

            @Override
            public void onDelAppointments(String preview_id, int position) {
                delAppointments(preview_id, position);
            }
        });
        mRecyclerView.setAdapter(mPreviewAdapter);
        getAppointments();
        mSwipeRefreshLayout.setRefreshing(true);
//        getPreviewList();

    }

    private void appointments(String preview_id, int position){
        XLApplication.getInstance().getRestClient().appointments(preview_id, new Callback<PreviewResponse>() {
            @Override
            public void success(PreviewResponse msgBean, Response response) {
                Toast.makeText(MyPreviewListActivity.this, "预约成功", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void failure(RetrofitError error) {
                Toast.makeText(MyPreviewListActivity.this, "预约失败", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void delAppointments(String preview_id, int position){
        XLApplication.getInstance().getRestClient().delAppointments(preview_id, new Callback<PreviewResponse>() {
            @Override
            public void success(PreviewResponse msgBean, Response response) {
                Toast.makeText(MyPreviewListActivity.this, "取消预约成功", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void failure(RetrofitError error) {
                Toast.makeText(MyPreviewListActivity.this, "取消预约失败", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void getAppointments(){
        XLApplication.getInstance().getRestClient().getAppointments(PreferencesStorageUtil.getUserId(MyPreviewListActivity.this), mCurrentPage, RestClient.DEFAULT_PREPAGE, new Callback<PreviewBean[]>() {
            @Override
            public void success(PreviewBean[] msgBean, Response response) {
                if (mCurrentPage == RestClient.DEFAULT_PAGE) {
                    mPreviewAdapter.refreshData(Arrays.asList(msgBean));
                } else {
                    mPreviewAdapter.addAll(Arrays.asList(msgBean));
                }
                if (msgBean.length < 20) {
                    mHasMoreData = false;
                }else{
                    mHasMoreData = true;
                }
                mSwipeRefreshLayout.setRefreshing(false);
                mSwipeRefreshLayout.setLoading(false);
            }

            @Override
            public void failure(RetrofitError error) {
                Toast.makeText(MyPreviewListActivity.this, "获取预约失败", Toast.LENGTH_SHORT).show();
            }
        });
    }

//    private List<PreviewBean> toPreviewList(PreviewResponse[] msgBean){
//
//    }


    @Override
    public void onRefresh() {
        mCurrentPage = RestClient.DEFAULT_PAGE;
//        getPreviewList();
    }

    @Override
    public void onLoad() {
        if(mHasMoreData){
            mCurrentPage ++;
//            getPreviewList();
        }else{
            mSwipeRefreshLayout.setRefreshing(false);
            mSwipeRefreshLayout.setLoading(false);
            Toast.makeText(MyPreviewListActivity.this, "没有更多数据", Toast.LENGTH_SHORT).show();
        }

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.parade_list_back:
                finish();
                break;
        }
    }
}
